package com.example.administrator.fishbone.bean;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

import com.example.administrator.fishbone.Fragment.HoneFragment.DynamicFragment;
import com.example.administrator.fishbone.db.DBOpenHeldper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${杨文超} on 2016/9/2.
 */
public class DaoTask {
    private final String Tag = this.getClass().getName();
    private SQLiteDatabase db;

    private DaoTask() {

    }
    private static class SingletonHolder{
        private static DaoTask dao = new DaoTask();
    }

    public static DaoTask instance() {
        return SingletonHolder.dao;
    }

    public void init(Context context) {
        String Name = "NewTask";
        if (!TextUtils.isEmpty(Name)) {
            DBOpenHeldper heldper = new DBOpenHeldper(context, Name, null, 1);
            db = heldper.getReadableDatabase();
        } else {
            Log.v("DaoStudent", "Name is empty!!!");
        }
    }

//    public boolean insert(DynamicFragment task) {
//        String insertSql = "INSERT INTO Task (context,name)\n"
//                +"VALUES(?,?)";
//        Object[] values = {task.getContext(), task.};
//        boolean result;
//        try {
//            if (mDb != null) {
//                mDb.execSQL(insertSql,values);
//            }
//            result = true;
//        } catch (SQLException e) {
//            Log.v(mTag,"insert error!!!"+ e.getMessage());
//            result = false;
//        }
//        return result;
//    }
public boolean insert(List<Person> tasks) {
    db.beginTransaction();
    boolean isSuccess = false;
    String insertSql = "INSERT INTO NewTask(last_name,title,content,time,name,number,Data1,Data2)\n" +
            "VALUES (?,?,?,?,?,?,?,?)";
    System.out.println("------>插入语句书写成功");
    try {
        for (Person tak : tasks) {
            Object[] values = {tak.getTextIcon(),
                    tak.getTitle(),tak.getContent(),
                    tak.getTime(),tak.getName(),
                    tak.getNumber(),tak.getData1(),
                    tak.getData2()};
            db.execSQL(insertSql,values);
        }
        db.setTransactionSuccessful();
        isSuccess = true;
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        db.endTransaction();
    }

    return isSuccess;
}

    public List<Person> queryAll() {
        if (db == null) {
            Log.v(Tag,"queryAll mDb id null!!!");
            return  null;
        }
        Cursor cursor = null;
        List<Person> tasks =null;
        try {
            cursor = db.rawQuery("SELECT * FROM NewTask",new String[]{});
            if (cursor != null) { //非空判断
                tasks = new ArrayList<>();
                int index = 0;
                //如果 cursor可以 移动到下一条数据
                while (cursor.moveToNext()) {
                   Person one  = new Person();
                    one.setTextIcon(cursor.getString(index));
                    one.setTitle(cursor.getString(index+1));
                    one.setContent(cursor.getString(index+2));
                    one.setTime(cursor.getString(index+3));
                    one.setName(cursor.getString(index+4));
                    one.setNumber(cursor.getString(index+5));
                    one.setData1(cursor.getString(index+6));
                    one.setData2(cursor.getString(index+7));
                    tasks.add(one);
                }
            }
        } catch (Exception e) {
            Log.e(Tag, "queryAll Error !!!" + e.getMessage());
        }finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return tasks;
    }

    public boolean delete(String id) {
        String deleteSql = "DELETE FROM NewTask WHERE _id = ?";
        if (db == null) {
            Log.e(Tag, "db is null !!!");
            return false;
        }
        boolean result;
        db.execSQL(deleteSql, new Object[]{id});
        result = true;
    return result;
    }



}

